package com.szt.mobile.inventory.modules.inventory.biz;

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.isoftstone.mis.ffair.util.log.FFLog;
import com.szt.mobile.inventory.common.biz.BaseBiz;
import com.szt.mobile.inventory.common.constants.IntfConstants;
import com.szt.mobile.inventory.common.entity.Assets;
import com.szt.mobile.inventory.common.entity.AssetsList;
import com.szt.mobile.inventory.common.entity.BaseEntity;
import com.szt.mobile.inventory.common.entity.Inventory;
import com.szt.mobile.inventory.common.entity.RequestEntity;
import com.szt.mobile.inventory.common.entity.ResponseEntity;
import com.szt.mobile.inventory.common.utils.GsonUtil;
import com.szt.mobile.inventory.common.utils.Xutils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by yanzhaodi on 2017/11/16.
 */

public class InventoryBiz extends BaseBiz {

    public static void getInventoryList(String startTime, String endTime, Xutils xutils, Xutils.XCallBack callback) {
        try {
            HashMap<String, Object> postjson = new HashMap<>();
            postjson.put("actioncode", "PDA003");
            postjson.put("GUID", System.currentTimeMillis());
            HashMap<String, String> postData = new HashMap<>();
            StringBuilder sb = new StringBuilder();
            if (!TextUtils.isEmpty(startTime)) {
                sb.append("'").append(startTime).append(" 00:00:00'");
            }

            if (!TextUtils.isEmpty(endTime)) {
                sb.append("'").append(endTime).append(" 23:59:59'");
            }
            postData.put("wherestr", sb.toString());
            postjson.put("postdata", postData);

            String json = GsonUtil.GsonString(postjson);
            Log.e("InventoryBiz", "获取盘点单发送报文:"+json);
            xutils.post(IntfConstants.BASE_URL, json, callback);

        } catch (Exception e) {
            FFLog.e(e, e.getMessage());
        }
    }

    public static void getAssets(List<String> autoIds, Xutils xutils, Xutils.XCallBack callback) {
        try {
            HashMap<String, Object> postjson = new HashMap<>();
            postjson.put("actioncode", "PDA002");
            postjson.put("GUID", System.currentTimeMillis());
            HashMap<String, String> postData = new HashMap<>();

            StringBuilder sb = new StringBuilder();
            for (String id : autoIds) {
                sb.append(id).append(",");
            }
            postData.put("applyCode", "007");
            postData.put("wherestr", " t2.id_ in (" + sb.substring(0, sb.length() - 1) + ")");
//            postData.put("wherestr", "");
            postjson.put("postdata", postData);
            String json = GsonUtil.GsonString(postjson);
            Log.e("InventoryBiz", "获取Assets发送报文:"+json);
            xutils.post(IntfConstants.BASE_URL, json, callback);
        } catch (Exception e) {
            FFLog.e(e, e.getMessage());
        }
    }

    public static void uploadInventory(List<Inventory> inventories, Xutils xutils, Xutils.XCallBack callback) {

        try {
            HashMap<String, Object> sendData = new HashMap<>();
            HashMap<String, Object> list = new HashMap<>();
            sendData.put("GUID", System.currentTimeMillis());
            sendData.put("actioncode", "PDA007");

            List<HashMap<String, Object>> inList = new ArrayList<>();
            for (Inventory inventory : inventories) {
                HashMap<String, Object> inMap = new HashMap<>();
                inMap.put("taskId", inventory.id);

                List<HashMap<String, String>> assetsList = new ArrayList<>();
                for (AssetsList as : inventory.detail) {
                    for (Assets a : as.asset) {

                        if (a.getStatus() != 1) {
                            HashMap<String, String> map = new HashMap<>();
                            map.put("AutoID", a.aUTOID);
                            map.put("asset70", a.asset70);
                            map.put("asset700", a.asset700+"");
                            map.put("asset240", a.asset240);
                            map.put("asset250", a.asset250);
                            map.put("status",a.status+"");
                            assetsList.add(map);
                        }
                    }
                }
                inMap.put("detail", assetsList);
                inList.add(inMap);
                list.put("list", inList);
                sendData.put("postdata", list);
            }
            String json = GsonUtil.GsonString(sendData);
            Log.e("InventoryBiz", "盘点单上传发送报文:"+json);
            xutils.post(IntfConstants.BASE_URL, json, callback);

        } catch (Exception e) {
            FFLog.e(e, e.getMessage());
        }
    }


}
